﻿/*-----------------------------------------------
// File: BattleViewCtrl.cs 
// Description: 战场渲染层，总控制，第一场战斗
// Author: Shaobing	492057342@qq.com
-----------------------------------------------*/
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using ProtoBuf.Message;
using AssetLoad;

namespace Battle.View
{
    /// <summary>
    /// 战场渲染层总控
    /// </summary>
    public class BattleViewFirstCtrl : BattleViewCtrl
    {
        public bool isFirstDialogShowed = false;

        public enum FirstBattleDialogEnum {
            _998,
            _999,
            Done
        }
        public FirstBattleDialogEnum dialogEnum = FirstBattleDialogEnum._998;

        public bool isFirstUltimateCanCast = false;

        public enum FirstBattleGuideStep {
            None,
            Begin,
            NormalSkillNeedFirstCast,
            NormalSkillFirstCasting,
            NormalSkillNeedSecondCast,
            NormalSkillSecondCasting,
            NormalSkillNeedAllCast,
            NormalSkillAllCasting,
            UltimateSkillNeedFirstCast,
            Over
        }

        public FirstBattleGuideStep curGuideStep = FirstBattleGuideStep.None;
        public string normalSkillCastFocus = "";
        public string ultimateSkillCastFocus = "";
        public bool isGuideShowing = false;

        public void SelectNormalSkillCastFocus() {
            foreach (var uid in GetActorUids(BattleCamp.Attacker)) {
                var bc = GetBattleCharacter(uid);
                if (bc.IsFirstSkillCanCast() && bc.IsSecondSkillCanCast()) {
                    normalSkillCastFocus = uid;
                    return;
                }
            }
            normalSkillCastFocus = "";
        }

        public void SelectUltimateSkillCastFocus(string uid) {
            ultimateSkillCastFocus = uid;
        }

        public override void Init(BattleManager m)
        {
            base.Init(m);
            isFirstDialogShowed = false;
            isFirstUltimateCanCast = false;
            curGuideStep = FirstBattleGuideStep.None;
            normalSkillCastFocus = "";
            isGuideShowing = false;
            dialogEnum = FirstBattleDialogEnum._998;
            
        }
        public override void InitState()
        {
            allStates = new Dictionary<BattleState, Fsm.FsmStateMonoBase<BattleState>>();
            allStates[BattleState.ResourceLoadState] = new FirstResourceLoadState(BattleState.ResourceLoadState, this);
            allStates[BattleState.EntranceState] = new EntranceState(BattleState.EntranceState, this);
            allStates[BattleState.BattleRunningState] = new FirstBattleRunningState(BattleState.BattleRunningState, this);
            allStates[BattleState.ResultState] = new ResultState(BattleState.ResultState, this);
        }

        public override bool isSkillCanCast4UI(BattleCharacter bc, SkillType st)
        {
            if (st == SkillType.Ultimate)
            {
                return base.isSkillCanCast4UI(bc, st) && isFirstUltimateCanCast;
            }
            else
            {
                return base.isSkillCanCast4UI(bc, st);
            }
        }

        public string firstStoryId {
            get {
                return "story_1";
            }
        }

        public GameObject storyObj;
        public string storyResourceName;
        public GameObject firstCGPanel;
        public void SkipStory()
        {
            storyObj.GetComponent<Story3dLogic>().Skip();
        }
        public void HideFirstCGPanel()
        {
            if (firstCGPanel != null)
            {
                firstCGPanel.GetComponent<FirstCGPanel>().Hide();
            }
        }


        public override IEnumerator LoadBase()
        {
            hztxPath = "particles/" + Dict.Blo.DictParticleBlo.GetCommonParticle("12").ResourcePath;
            LoadBuffTransform();
            yield return StartCoroutine(BattleFactory.RegistCommon());
            LoadShadow();

            AssetBundleManager.Instance.LoadAssetBundle(hztxPath);
            while (!AssetBundleManager.Instance.IsLoadedAssetBundle(hztxPath))
            {
                yield return 1;
            }
            changeFormationEffect = Instantiate(AssetBundleManager.Instance.GetAssetFromBundle(hztxPath, "UI_huanzhen", typeof(GameObject))) as GameObject;
            changeFormationEffect.transform.localPosition = Vector3.zero;
            changeFormationEffect.SetActive(false);
            CommonMethod.ChangeLayer(changeFormationEffect.transform, LayerName.SceneParticle);
            bufferResourceCache = BufferResourceCache.Initialize();
            yield return StartCoroutine(AudioManager.Instance.LoadAudioResource("SE_dead"));


            var firstpanel = ResourceManager.Instance.LoadResource("Pages/FirstCGPanel");
            if (firstpanel != null)
            {
                firstCGPanel = GameObject.Instantiate(firstpanel) as GameObject;
                firstCGPanel.transform.SetParent(BattleManager.Instance.battlePanel.transform.parent);
                firstCGPanel.transform.localPosition = Vector3.zero;
                firstCGPanel.transform.localScale = Vector3.one;
                firstCGPanel.transform.localEulerAngles = Vector3.zero;
                firstCGPanel.GetComponent<FirstCGPanel>().Init(this);
            }

            var dict = Dict.Blo.Dict3dStoryBlo.GetTable3dStory(firstStoryId);
            if (dict != null)
            {
                storyResourceName = dict.StoryResourceName;
                yield return StartCoroutine(ResourceLoader.GetInstance().story3dLoader.LoadStory(storyResourceName, (asset) => {
                    GameObject obj = asset as GameObject;
                    if (obj != null)
                    {
                        obj.transform.SetParent(BattleManager.Instance.transform);
                        obj.transform.localPosition = Vector3.zero;
                        obj.transform.localScale = Vector3.one;
                        obj.transform.localEulerAngles = new Vector3(0, -180, 0);
                        obj.SetActive(false);

                        storyObj = obj;
                    }
                }));
            }
            IsBattleBaseLoaded = true;
        }
        public override void UnloadBase()
        {
            base.UnloadBase();

            if (firstCGPanel != null)
            {
                GameObject.Destroy(firstCGPanel);
            }
        }


        public override void OnBattleCharacterEnterAttackWaitState(BattleCharacter bc)
        {
            if (curGuideStep == FirstBattleGuideStep.NormalSkillFirstCasting && bc.uniqueId == normalSkillCastFocus) {
                curGuideStep = FirstBattleGuideStep.NormalSkillNeedSecondCast;
                Time.timeScale = 0f;
                BattleManager.Instance.battlePanel.guidePanel.Show(BattleManager.Instance.battlePanel.GetBattleHeroItem(normalSkillCastFocus),
                    curGuideStep);
                isGuideShowing = true;
            }
            base.OnBattleCharacterEnterAttackWaitState(bc);
        }
    }

}